package dmt16.TelemetrySuit.UI.Tiles
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.ISensorType;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorEvent;
	import dmt16.TelemetrySuit.Controller.SensorTypes.ThermistorSensor;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Colours;
	import dmt16.TelemetrySuit.UI.PlotTools.Graph;
	import dmt16.TelemetrySuit.UI.PlotTools.GraphLine;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class ThermistorTile extends TileBase
	{
		public function ThermistorTile(sensor:ThermistorSensor=null)
		{
			super(sensor);
			_tileGFX=new Sprite();
			tileContent=_tileGFX;
			_gtxt=new TextField();
			_gtxt.embedFonts=true;
			_gtxt.selectable=false;
			_gtxt.defaultTextFormat=_textFormat;
			_gtxt.x=0;
			_gtxt.width=WIDTH;
			_gtxt.y=60*HEIGHT/300;
			_gtxt.text="0.0C";
			
			var titleText:TextField=new TextField();
			titleText.embedFonts=true;
			titleText.selectable=false;
			titleText.defaultTextFormat=_textFormat3;
			titleText.x=0;
			titleText.y=10*HEIGHT/300;
			titleText.width=WIDTH;
			titleText.height=155*HEIGHT/300;
			titleText.text="temperature";
			addChild(titleText);
			
			_detailView=new Graph();
			detailContent=_detailView;
			
			_thermistorSensor=sensor;
			_thermistorSensor.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onSensorData);
			
			_dataLine=new GraphLine(sensor.data);
			_dataLine.lineStyle.color=Colours.C1;
			
			_detailView.addLine(_dataLine);
			
			addChild(_gtxt);
		}
		
		private var _detailView:Graph;
		private var _dataLine:GraphLine;
		
		private var _thermistorSensor:ThermistorSensor;
		
		private var _tileGFX:Sprite;
		private var _gtxt:TextField;
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=75*HEIGHT/300;
			_textFormat.align=TextFormatAlign.CENTER;
			_textFormat.color=Colours.FG;
		}
		private static var _textFormat2:TextFormat;
		{
			_textFormat2=new TextFormat();
			_textFormat2.font=Fonts.DSDigital;
			_textFormat2.size=55*HEIGHT/300;
			_textFormat2.align=TextFormatAlign.CENTER;
			_textFormat2.color=Colours.FG;
		}
		private static var _textFormat3:TextFormat;
		{
			_textFormat3=new TextFormat();
			_textFormat3.font=Fonts.DSDigitalBold;
			_textFormat3.size=45*HEIGHT/300;
			_textFormat3.align=TextFormatAlign.CENTER;
			_textFormat3.color=Colours.FG;
		}
		
		public override function drawDetailContent():void{
			_detailView.fullRedraw();
		}
		
		private function _onSensorData(event:SensorEvent):void{
			var temp:Number=_thermistorSensor.data[_thermistorSensor.data.length-1];
			_gtxt.text=temp.toFixed(1)+"C";
			_gtxt.setTextFormat(_textFormat2,_gtxt.length-1,_gtxt.length);
		}
	}
}